/*
 * 版权所有(c)  1952110078@qq.com  2022 - 2022
 */

package com.ldc.module_main.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ldc.commonlibs.common.RspListModel;
import com.ldc.commonlibs.ui.VMFragmentBase;
import com.ldc.commonlibs.utils.DataSourceUtil;
import com.ldc.commonlibs.utils.ViewModelUtil;
import com.ldc.module_main.adaapter.BannerListAdapter;
import com.ldc.module_main.adaapter.PersonalizedAdapter;
import com.ldc.module_main.adaapter.RecommendListAdapter;
import com.ldc.module_main.databinding.MainFragmentBannerCarBinding;
import com.ldc.module_main.model.BannerModel;
import com.ldc.module_main.model.DragonBollModel;
import com.ldc.module_main.model.PersonalizedModel;
import com.ldc.module_main.viewmodel.BannerCarViewModel;

import java.util.List;


public class BannerCarFragment extends VMFragmentBase<MainFragmentBannerCarBinding, BannerCarViewModel> {
    private BannerListAdapter bannerListAdapter;
    private RecommendListAdapter recommendListAdapter;
    private PersonalizedAdapter personalizedAdapter;

    @Override
    protected void main(@Nullable Bundle savedInstanceState) {
        initBannerListAdapter();
        initRecommendListAdapter();
        initPersonalizedAdapter();
        iniData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.getDragonBoll().removeObserver(this::getRecommendList);
        viewModel.getBannerList().removeObserver(this::getBannerList);
        viewModel.getPersonalizedModel().removeObserver(this::getPersonalizeList);
    }

    @Override
    protected MainFragmentBannerCarBinding initViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return MainFragmentBannerCarBinding.inflate(inflater, container, false);
    }

    @Override
    protected BannerCarViewModel initViewModel() {
        return ViewModelUtil.create(this, BannerCarViewModel.class);
    }

    private void initBannerListAdapter() {
        bannerListAdapter = new BannerListAdapter();
        viewBinding.bannerRv.setAdapter(bannerListAdapter);
        viewBinding.bannerRv.setCurrentItem(0);
    }

    private void initRecommendListAdapter() {
        recommendListAdapter = new RecommendListAdapter();
        viewBinding.recommendRv.setHasFixedSize(true);
        viewBinding.recommendRv.setLayoutManager(new LinearLayoutManager(requireActivity(), LinearLayoutManager.HORIZONTAL, false));
        viewBinding.recommendRv.setAdapter(recommendListAdapter);
    }

    private void initPersonalizedAdapter() {
        personalizedAdapter = new PersonalizedAdapter();
        viewBinding.rvRecommendedSongList.setLayoutManager(new LinearLayoutManager(requireActivity(), LinearLayoutManager.HORIZONTAL, false));
        viewBinding.rvRecommendedSongList.setHasFixedSize(true);
        viewBinding.rvRecommendedSongList.setAdapter(personalizedAdapter);
    }


    private void iniData() {
        viewModel.getDragonBoll().observeForever(this::getRecommendList);
        viewModel.getBannerList().observeForever(this::getBannerList);
        viewModel.getPersonalizedModel().observeForever(this::getPersonalizeList);
        viewModel.getBanner();
        viewModel.queryDragonBoll();
        viewModel.personalized();

    }

    private void getRecommendList(RspListModel<DragonBollModel> data) {
        if (data == null) {
            return;
        }
        recommendListAdapter.addData(data.getData());
    }

    private void getBannerList(List<BannerModel> data) {
        if (data == null) {
            return;
        }
        bannerListAdapter.addData(data);
    }

    private void getPersonalizeList(PersonalizedModel data) {
        if (data == null) {
            return;
        }
        personalizedAdapter.addNewData(data.getResult());

    }
}